$(function () {
    var client_datagrid = $("#client_datagrid");
    var client_dialog = $("#client_dialog");
    var client_form = $("#client_form");
    var changeClient_dialog=$("#changeClient_dialog");
    var changeClient_form=$("#changeClient_form");

    var cmdObj = {
        //工具栏的刷新
        reload: function () {
            client_datagrid.datagrid("reload");
        },
        //编辑
        edit: function () {
            //判断是否选中数据
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            //修改回显属性
            if(row){
                //修改职业属性
                row["job.id"]=row.job.id;
                //修改客户来源属性
                row["resource.id"]=row.resource.id;
                //修改收入水平属性
                row["income.id"]=row.income.id;
            }
            //添加与编辑时将数据清空
            client_form.form("clear");
            //数据回显: 同名原则
            client_form.form("load", row);
            //1.弹框
            client_dialog.dialog("open");
            //设置标题
            client_dialog.dialog("setTitle", "客户信息编辑");
        },
        //吸纳客户
        changeState: function () {
            //判断是否选中数据
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            row.state=1;
            $.messager.confirm('确认', '您确认想要执行这个操作吗？', function (r) {
                if (r) {
                    $.get("/client/changeState", {id: row.id,state:row.state}, function (data) {
                        if (data.success) {

                            //刷新列表数据
                            client_datagrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    })
                }
            });
        },
        //保存操作
        save: function () {
            var id = $("#clientId").val();
            url = "/client/update";
            client_form.form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);

                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        //关闭框
                        client_dialog.dialog("close");
                        //刷新列表数据
                        client_datagrid.datagrid("reload");
                    }
                }
            })
        },
        //关闭
        cancel: function () {
            client_dialog.dialog("close");
        },
    }

    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    client_datagrid.datagrid({
        url: "clientresources/list",
        fit: true,
        fitColumns: true,
        striped: true,
        singleSelect: true,
        pagination: true,
        rownumbers: true,
        modal:true,
        toolbar: '#tb',
        columns: [[
            {field: "ck", checkbox: true},//显示多选框样式
            {field: "id", hidden: true},
            {field: "createDate", title: "创建日期", width: 100,sortable: true},
            {
                field: "createUser", title: "创建人", width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return value.username;
                    } else {
                        return "创建人名字录入失败";
                    }
                }
            },
            {
                field: "leadingUser", title: "负责人", width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return value.username;
                    } else {
                        return "负责人名字录入失败";
                    }
                }
            },
            {field: "name", title: "客户姓名", width: 100},
            {field: "age", title: "年龄", width: 100},
            {
                field: "gender", title: "性别", width: 100, formatter: function (value, row, index) {
                    if (value == 1) {
                        return "男";
                    } else if (value == 0) {
                        return "女";
                    }else{
                        return "未填写性别";
                    }
                }
            },
            {
                field: "job", title: "职业", width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return value.name;
                    } else {
                        return "未填写职业信息";
                    }
                }
            },
            {
                field: "resource", title: "客户来源", width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return value.name;
                    } else {
                        return "未填写客户来源信息";
                    }
                }
            },
            {
                field: "income", title: "薪资水平", width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return value.name;
                    } else {
                        return "未填写薪资水平信息";
                    }
                }
            },
            {field: "email", title: "邮箱", width: 100},
            {field: "tel", title: "电话", width: 100},
            {field: "address", title: "客户地址", width: 100},
            {field: "weixin", title: "微信", width: 100},
            {field: "idNumber", title: "身份证号码", width: 100},
            {
                field: "state", title: "客户开发标识", width: 100, formatter: function (value, row, index) {
                    if (value == 0) {
                        return "<font color='green'>正式客户</font>";
                    } else if (value == 1) {
                        return "可开发客户";
                    }else if(value == 2){
                        return "<font color='red'>建议转入资源池客户</font>";
                    }else if(value == 3){
                        return "资源池客户";
                    }else if(value == 4){
                        return "计划开发客户";
                    }else if(value == 5){
                        return "<font color='#6495ed'>待签单客户</font>";
                    }
                }
            },
            {field: "remark", title: "备注", width: 100}
        ]],
    });
    client_dialog.dialog({
        width: 450,
        height: 550,
        buttons: "#bb",
        closed: true,
        resizable:true,
        draggable:true
    })
})


//工具栏的高级查询
function query() {

    /*关键字查询*/
    var ls = $("#leadingSreach").textbox("getValue");
    /*负责人任查询*/
    var kw = $("#keyword").textbox("getValue");

    /*根据创建时间查询*/
    /*开始时间*/
    var bd = $("#beginDate").textbox("getValue");
    /*结束时间*/
    var ed = $("#endDate").textbox("getValue");
    $("#client_datagrid").datagrid("load", {
        keyword: kw,
        leadingSreach: ls,
        beginDate: bd,
        endDate: ed
    });
}
